﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseObj : GraspingObjectBase3D
{
    [HideInInspector]
    public bool isChecked;//是否被检查过
    public bool isCanChecked;//该道具是否需要检查
    public int StepOneId;//实验步骤1小步骤id
    public string Name = "";
    [HideInInspector]
    public bool isCatch;
    public bool canRound = false;

    public Vector3 positon;
    public bool canCatsh = true;

    public void Start()
    {
        positon = transform.position;
    }

    public virtual void ResetPosition()
    {
        //transform.localEulerAngles = new Vector3(0, 0, 0);
        transform.position = positon;
        GetComponent<Rigidbody>().isKinematic = false;
    }

    public override void OnInit()
    {
        base.OnInit();
    }

    public Collider GetCollider()
    {
        return m_collider;
    }

    public Rigidbody GetRigidbody()
    {
        return m_rigidbody;
    }
    public override void OnHandleTrigger(Transform camera, NoloDeviceType handleType)
    {
        if (BigStepsManager.IsStart) return;
        if (!canCatsh) return;
        isCatch = true;
        base.OnHandleTrigger(camera, handleType);
        GetComponent<Rigidbody>().useGravity = false;
        GetComponent<Rigidbody>().isKinematic = true;
    }
    public override void OnHoldingTrigger(Ray newRay)
    {
        if (BigStepsManager.IsStart) return;
        if (!canCatsh) return;
        base.OnHoldingTrigger(newRay);
        GetComponent<Rigidbody>().useGravity = false;
        GetComponent<Rigidbody>().isKinematic = true;
        if (canRound)
        {
            if (mHandleType == NoloDeviceType.LeftController)
            {
                Quaternion var = NoloVR_Controller.GetDevice(NoloDeviceType.LeftController).GetPose().rot;
                Vector3 angle = var.eulerAngles;
                Vector3 value = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, angle.z);
                transform.eulerAngles = value;
            }
            else if (mHandleType == NoloDeviceType.RightController)
            {
                Quaternion var = NoloVR_Controller.GetDevice(NoloDeviceType.RightController).GetPose().rot;
                Vector3 angle = var.eulerAngles;
                Vector3 value = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, angle.z);
                transform.eulerAngles = value;
            }
        }
    }
    public override void OnReleseTrigger(NoloDeviceType handleType)
    {
        if (BigStepsManager.IsStart) return;
        base.OnReleseTrigger(handleType);
        isCatch = false;
        GetComponent<Rigidbody>().useGravity = true;
        GetComponent<Rigidbody>().isKinematic = false;
        if (transform.position.y < 0.45f)
        {
            ResetPosition();
        }
        Vector3 pos = Camera.main.WorldToViewportPoint(this.transform.position);
        if (pos.x < 0 || pos.x > 1 || pos.y < 0 || pos.y > 1)
        {
            ResetPosition();
        }
    }
    public override void OnRefreshHandle()
    {
        base.OnRefreshHandle();
    }
    public override void OnRefresh()
    {
        base.OnRefresh();
    }
    protected override void OnDestroyThis()
    {
        base.OnDestroyThis();
    }

    public override void OnRayEnter()
    {
        base.OnRayEnter();
        // //开启外发光
        // if (m_ho.Length > 0)
        // {
        //     for (int i = 0; i < m_ho.Length; i++)
        //     {
        //         m_ho[i].ConstantOn(Color.green);
        //     }
        // }
        if (!isChecked && isCanChecked)
        {
            StepsManager.Instance.StepOneNum[StepOneId] = true;
            StepsManager.Instance.SetStepOne(StepOneId);
            isChecked = true;
            if (StepsManager.Instance.getStepOneState())
            {
            }
        }
    }

    public override void OnRayExit()
    {
        base.OnRayExit();
        // //关闭持续外发光
        // if (m_ho.Length > 0)
        // {
        //     for (int i = 0; i < m_ho.Length; i++)
        //     {
        //         m_ho[i].ConstantOff();
        //     }
        // }
    }

    private void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.name == "Bucket")
        {
            if (transform.position.y < other.transform.position.y)
                ResetPosition();
        }
        if (other.gameObject.gameObject.name == "IronPlatform" && name == "Rig_Cloth")
        {
            ResetPosition();
        }
    }
}
